_ = require('underscore')
pinyin = require('pinyin')

page_bwss = (state={},action)->
  switch action.type
    when 'BWSS/GET_LIST_START'
      Object.assign {},state,{isLoading:true}
    when 'BWSS/GET_LIST_COMPLETED'
      debugger
      Object.assign {},state,{
        list:state.list.concat(action.payload.list)
        isLoading:false
        pageIndex:if action.payload.list.length > 0 then state.pageIndex+1 else state.pageIndex
        completed:action.payload.list.length < 20
      }
    when 'BWSS/REFRESH_LIST_START'
      Object.assign {},state,{
        list:[]
        isLoading:false
        refreshing:true
      }
    when 'BWSS/REFRESH_LIST_COMPLETED'
      debugger
      list = _.groupBy action.payload.list,(m)->
        str = m.VCPLACESNAME[0]
        if str is '孚'
          return 'F'
        if str is '浜'
          return 'B'
        if str is '淞'
          return 'S'
        if str is '漕'
          return 'C'
        if str is '虬'
          return 'Q'
        if str is '闵'
          return 'M'

        x = (pinyin str,
              style:pinyin.STYLE_FIRST_LETTER)
        x = x[0][0]
        x.toUpperCase()
      Object.assign {},state,{
        list:list
        isLoading:false
        pageIndex:1
        refreshing:false
        lastFetchTime:action.payload.lastFetchTime
        completed:true
      }
    else
      state

module.exports = page_bwss
